package com.ruoyi.process.pipemaintainence2.service.impl;

import java.util.*;

import com.ruoyi.common.core.page.Page;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.id.IdGenerator;
import com.ruoyi.common.utils.BeanUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.process.dynamic.service.BpmnDynamicService;
import com.ruoyi.process.pipemaintainence.domain.DrainagePipeMaintainence;
import com.ruoyi.process.pipemaintainence.service.IDrainagePipeMainService;
import com.ruoyi.process.pipemaintainence2.domain.TBpmnCopyProcess;
import com.ruoyi.process.pipemaintainence2.service.ITBpmnCopyProcessService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.process.pipemaintainence2.mapper.TDrainagePipeMaintainenceMapper;
import com.ruoyi.process.pipemaintainence2.domain.TDrainagePipeMaintainence;
import com.ruoyi.process.pipemaintainence2.service.ITDrainagePipeMaintainenceService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 排水管养Service业务层处理
 *
 * @author Xianlu Tech
 * @date 2019-12-05
 */
@Service
public class TDrainagePipeMaintainenceServiceImpl implements ITDrainagePipeMaintainenceService {

    @Autowired
    private IdGenerator idGenerator;

    @Autowired
    private TDrainagePipeMaintainenceMapper tDrainagePipeMaintainenceMapper;

    @Autowired
    private BpmnDynamicService bpmnDynamicService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private IDrainagePipeMainService drainagePipeMainService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ITBpmnCopyProcessService bpmnCopyProcessService;

    /**
     * 查询排水管养
     *
     * @param id 排水管养ID
     * @return 排水管养
     */
    @Override
    public TDrainagePipeMaintainence selectTDrainagePipeMaintainenceById(String id) {
        return tDrainagePipeMaintainenceMapper.selectTDrainagePipeMaintainenceById(id);
    }

    /**
     * 查询排水管养列表
     *
     * @param tDrainagePipeMaintainence 排水管养
     * @return 排水管养
     */
    @Override
    public List<TDrainagePipeMaintainence> selectTDrainagePipeMaintainenceList(TDrainagePipeMaintainence tDrainagePipeMaintainence) {
        return tDrainagePipeMaintainenceMapper.selectTDrainagePipeMaintainenceList(tDrainagePipeMaintainence);
    }


    /**
     * 查询排水管养列表
     *
     * @param tDrainagePipeMaintainence 排水管养
     * @return 排水管养
     */
    public List<Map<String, Object>> selectTDrainagePipeMaintainenceList2(TDrainagePipeMaintainence tDrainagePipeMaintainence) {
        List<Map<String, Object>> maps = new ArrayList<>();
        List<TDrainagePipeMaintainence> dpList = tDrainagePipeMaintainenceMapper.selectTDrainagePipeMaintainenceList(tDrainagePipeMaintainence);
        if (BeanUtils.isNotEmpty(dpList)) {
            for (TDrainagePipeMaintainence td : dpList) {
                Map<String, Object> map = new HashMap<>();
                map.put("area", td.getArea());
                map.put("contact", td.getContact());
                map.put("length", td.getLength());
                map.put("issuePlace", td.getIssuePalce());
                map.put("memberArea", td.getMemberArea());
                map.put("problemSources", td.getProblemSources());
                map.put("telephone", td.getTelephone());
                maps.add(map);
            }
        }
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectTDrainagePipeMaintainenceList3(Map<String, Object> paramMap) {
        List<Map<String, Object>> mapList = tDrainagePipeMaintainenceMapper.selectTDrainagePipeMaintainenceList3(paramMap);
        //需不需要处理
        if (BeanUtils.isNotEmpty(mapList)) {
            for (Map map : mapList) {
                String processInstanceId = (String) map.get("processInstanceId");
                if (BeanUtils.isNotEmpty(processInstanceId)) {
                    List<Task> taskList = taskService.createTaskQuery()
                            .processInstanceId(processInstanceId).list();
                    if (BeanUtils.isNotEmpty(taskList)) {
                        //获取到任务的最后一个任务的任务名称
                        map.put("taskName", taskList.get(taskList.size() - 1).getName());
                        map.put("taskId", taskList.get(taskList.size() - 1).getId());
                    } else {
                        map.put("taskName", "已办结");
                    }
                } else {
                    map.put("taskName", "未启动");
                }
            }
        }
        return mapList;
    }


    /**
     * 新增排水管养
     *
     * @param tDrainagePipeMaintainence 排水管养
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTDrainagePipeMaintainence(TDrainagePipeMaintainence tDrainagePipeMaintainence) {
        String userId = ShiroUtils.getUserId().toString();
        String pipeId = idGenerator.getId();
        //新增抄送
        String copyIds = tDrainagePipeMaintainence.getCopyIds();
        //切割
        if (StringUtils.isNotEmpty(copyIds)) {
            String[] ids = StringUtil.split(copyIds, ",");
            for (String id : ids) {
                TBpmnCopyProcess copyProcess = new TBpmnCopyProcess();
                copyProcess.setId(idGenerator.getId());
                copyProcess.setCreateBy(userId);
                copyProcess.setCreateTime(new Date());
                copyProcess.setBussinessKey(pipeId);
                copyProcess.setUserId(id);
                bpmnCopyProcessService.insertTBpmnCopyProcess(copyProcess);
            }
        }
        tDrainagePipeMaintainence.setCreateTime(DateUtils.getNowDate());
        //新增设置主键id
        tDrainagePipeMaintainence.setId(pipeId);
        tDrainagePipeMaintainence.setCreateBy(userId);
        return tDrainagePipeMaintainenceMapper.insertTDrainagePipeMaintainence(tDrainagePipeMaintainence);

    }

    /**
     * 修改排水管养
     *
     * @param tDrainagePipeMaintainence 排水管养
     * @return 结果
     */
    @Override
    public int updateTDrainagePipeMaintainence(TDrainagePipeMaintainence tDrainagePipeMaintainence) {
        tDrainagePipeMaintainence.setUpdateTime(DateUtils.getNowDate());
        return tDrainagePipeMaintainenceMapper.updateTDrainagePipeMaintainence(tDrainagePipeMaintainence);
    }

    /**
     * 删除排水管养对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTDrainagePipeMaintainenceByIds(String ids) {
        return tDrainagePipeMaintainenceMapper.deleteTDrainagePipeMaintainenceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除排水管养信息
     *
     * @param id 排水管养ID
     * @return 结果
     */
    @Override
    public int deleteTDrainagePipeMaintainenceById(String id) {
        return tDrainagePipeMaintainenceMapper.deleteTDrainagePipeMaintainenceById(id);
    }


    @Override
    public List<Map<String, Object>> todoTaskList(Map<String, Object> paramMap) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String userId = ShiroUtils.getSysUser().getUserId().toString();
        Page<Map<String, Object>> page = bpmnDynamicService.todoTaskList(pageNum, pageSize, userId);
        List<Map<String, Object>> result = page.getResult();
        return result;
    }

    @Override
    public List<Map<String, Object>> doneTask() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String userId = ShiroUtils.getSysUser().getUserId().toString();
        Page<Map<String, Object>> page = bpmnDynamicService.finishedByAssignee(pageNum, pageSize, userId);
        List<Map<String, Object>> result = page.getResult();
        return result;
    }

    @Override
    public List<Map<String, Object>> startByOwn() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        String userId = ShiroUtils.getSysUser().getUserId().toString();
        Page<Map<String, Object>> page = bpmnDynamicService.findBpmnInstByStartUserId(pageNum, pageSize, userId);
        List<Map<String, Object>> result = page.getResult();
        return result;
    }

    @Override
    @Transactional
    public void submitApply(TDrainagePipeMaintainence tDrainagePipeMaintainence, String applyUserId) {
        DrainagePipeMaintainence drainagePipeMaintainence = new DrainagePipeMaintainence();
        org.springframework.beans.BeanUtils.copyProperties(tDrainagePipeMaintainence, drainagePipeMaintainence);
        //获取流程实例id
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("pipeMaintainence2").
                orderByProcessDefinitionVersion().desc().list().get(0);
        drainagePipeMainService.start(drainagePipeMaintainence, applyUserId, processDefinition.getId());
    }


    @Override
    public void complete(String userId, String taskId, Boolean opinion, String comment) {
        drainagePipeMainService.completeTask(userId, taskId, opinion, comment);
    }

}

